package com.cardone.persistent.datasource;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by yaohaitao on 2014/10/28.
 */
@lombok.extern.slf4j.Slf4j
public class CardOneRoutingDataSource extends org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource {
    /**
     * 当前key
     */
    private static ThreadLocal<String> determineCurrentLookupKey = new ThreadLocal<>();

    public static String getDetermineCurrentLookupKey() {
        return determineCurrentLookupKey.get();
    }

    public static void setDetermineCurrentLookupKey(String determineCurrentLookupKey) {
        if (StringUtils.isBlank(determineCurrentLookupKey)) {
            CardOneRoutingDataSource.determineCurrentLookupKey.remove();

            return;
        }

        CardOneRoutingDataSource.determineCurrentLookupKey.set(determineCurrentLookupKey);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        String determineCurrentLookupKey = getDetermineCurrentLookupKey();

        return determineCurrentLookupKey;
    }
}
